package com.thm.tfast.modules.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.thm.tfast.core.exception.Asserts;
import com.thm.tfast.core.sevruity.component.DynamicSecurityMetadataSource;
import com.thm.tfast.core.sevruity.domain.SysUserDetails;
import com.thm.tfast.core.utils.SecurityUtil;
import com.thm.tfast.core.utils.SpringUtil;
import com.thm.tfast.modules.sys.mapper.SysResourceMapper;
import com.thm.tfast.modules.sys.model.SysResource;
import com.thm.tfast.modules.sys.model.dto.resource.ResourceInsertDto;
import com.thm.tfast.modules.sys.model.dto.resource.ResourceUpdateDto;
import com.thm.tfast.modules.sys.model.vo.resource.ResourceVo;
import com.thm.tfast.modules.sys.service.SysCacheService;
import com.thm.tfast.modules.sys.service.SysResourceService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 资源信息 服务实现类
 * </p>
 *
 * @author THM
 * @since 2022-10-10
 */
@Service
public class SysResourceServiceImpl extends ServiceImpl<SysResourceMapper, SysResource> implements SysResourceService {
    @Resource
    private SysCacheService sysCacheService;
    @Override
    public List<ResourceVo> getAllResource() {
        List<ResourceVo> resourceVoList = sysCacheService.getResource();
        if (CollectionUtil.isEmpty(resourceVoList)){
            resourceVoList = this.baseMapper.getAllResource();
            sysCacheService.setResource(resourceVoList);
        }
        return resourceVoList;
    }

    @Override
    public Page<SysResource> page(String keyword, Long categoryId, Integer pageNum, Integer pageSize) {
        Page<SysResource> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SysResource> wrapper = Wrappers.lambdaQuery();
        if (StrUtil.isNotEmpty(keyword)) {
            wrapper.like(SysResource::getName, keyword)
                    .or()
                    .like(SysResource::getUrl, keyword);
        }
        if (Objects.nonNull(categoryId)){
            wrapper.eq(SysResource::getCategoryId,categoryId);
        }
        return this.page(page, wrapper);
    }

    @Override
    @Transactional
    public Long insert(ResourceInsertDto dto) {
        SysResource sysResource = this.getOne(Wrappers.<SysResource>lambdaQuery().eq(SysResource::getName, dto.getName()));
        if (Objects.nonNull(sysResource)){
            Asserts.fail("资源已经存在！");
        }
        sysResource = BeanUtil.toBean(dto,SysResource.class);
        SysUserDetails currentUser = SecurityUtil.getCurrentUser();
        sysResource.setCreateUser(currentUser.getUserId());
        sysResource.setCreateTime(currentUser.getDate());
        sysResource.setUpdateUser(currentUser.getUserId());
        sysResource.setUpdateTime(currentUser.getDate());
        this.save(sysResource);
        return sysResource.getId();
    }

    @Override
    @Transactional
    public Boolean update(ResourceUpdateDto dto) {
        if (Objects.isNull(this.getById(dto.getId()))){
            Asserts.fail("资源不存在");
        }
        SysResource sysResource = BeanUtil.toBean(dto, SysResource.class);
        SysUserDetails currentUser = SecurityUtil.getCurrentUser();
        sysResource.setUpdateUser(currentUser.getUserId());
        sysResource.setUpdateTime(currentUser.getDate());
        //删除资源缓存
        delResourceCache();
        return this.updateById(sysResource);
    }

    @Override
    @Transactional
    public Boolean delete(Long resourceId) {
        SysResource sysResource = this.getById(resourceId);
        if (Objects.isNull(sysResource)){
            Asserts.fail("资源不存在");
        }
        //删除资源缓存
        delResourceCache();
        return this.removeById(resourceId);
    }

    private void delResourceCache(){
        //删除资源缓存
        sysCacheService.delResource();
        //删除动态权限资源缓存
        DynamicSecurityMetadataSource dynamicSecurityMetadataSource = SpringUtil.getBean(DynamicSecurityMetadataSource.class);
        dynamicSecurityMetadataSource.clearDataSource();
    }
}
